package frontend_package;

import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import backend_package.Analyst;
import backend_package.Notes;
import backend_package.Packet;

/**
 * This class creates a GUI that lets users add notes to their private files
 * 
 * @author Zheng Yang date 12-1-14
 * 
 * */
@SuppressWarnings("serial")
public class AddQuestionsPanel extends JPanel implements Observer {
	/**Path to dbfiles folder in Temp directory.*/
	private String dbfilesPath;

	private JTextField textField;
	private JTextField textField_1;
	private String myUserName;
	private ArrayList<Notes> notes;

	/**
	 * add new question or answers.
	 * 
	 * @author Zheng Yang
	 * 
	 */
	public AddQuestionsPanel() {
		dbfilesPath = reverseFWDSlashes(System.getProperty("java.io.tmpdir")) + "dbfiles/"; 
		Dimension panelDimension = new Dimension(1000, 650);
		this.setPreferredSize(panelDimension); 
		notes = new ArrayList<Notes>();
		setBackground(SystemColor.inactiveCaptionBorder);

		JLabel lblNewLabel = new JLabel("New Question? *");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Tempus Sans ITC", Font.BOLD, 18));

		textField = new JTextField();
		textField.setColumns(10);

		JLabel lblAnswer = new JLabel("Answers (optional)");
		lblAnswer.setHorizontalAlignment(SwingConstants.CENTER);
		lblAnswer.setFont(new Font("Tempus Sans ITC", Font.BOLD, 18));

		textField_1 = new JTextField();
		textField_1.setColumns(10);

		JButton btnNewButton = new JButton("POST");
		/**
		 * @author Eva Revear
		 * 
		 *         This action listener attaches to the post button and
		 *         transmits the text from the panel into a note object post new
		 *         questions or answer online.
		 */
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String question = textField.getText();

				String optionalAnswer = textField_1.getText();

				Notes note = new Notes(question, optionalAnswer);

				try {
					addNote(note);

				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		btnNewButton.setFont(new Font("Tempus Sans ITC", Font.BOLD, 15));

		JButton btnOpenTxtFile = new JButton("SAVED NOTES");
		btnOpenTxtFile.addActionListener(new ActionListener() { // open txt file
																// of personal
																// Note.
					public void actionPerformed(ActionEvent arg0) {
						File file = new File(dbfilesPath + myUserName
								+ "/notes.txt");
						try {
							Desktop.getDesktop().open(file);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
		btnOpenTxtFile.setFont(new Font("Tempus Sans ITC", Font.BOLD, 15));
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addGap(47)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																lblAnswer,
																GroupLayout.PREFERRED_SIZE,
																268,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																textField,
																GroupLayout.PREFERRED_SIZE,
																535,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblNewLabel,
																GroupLayout.PREFERRED_SIZE,
																268,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																groupLayout
																		.createParallelGroup(
																				Alignment.LEADING)
																		.addGroup(
																				groupLayout
																						.createSequentialGroup()
																						.addComponent(
																								btnOpenTxtFile,
																								GroupLayout.PREFERRED_SIZE,
																								160,
																								GroupLayout.PREFERRED_SIZE)
																						.addGap(75)
																						.addComponent(
																								btnNewButton,
																								GroupLayout.PREFERRED_SIZE,
																								160,
																								GroupLayout.PREFERRED_SIZE))
																		.addComponent(
																				textField_1,
																				GroupLayout.PREFERRED_SIZE,
																				535,
																				GroupLayout.PREFERRED_SIZE)))
										.addContainerGap(82, Short.MAX_VALUE)));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addGap(41)
										.addComponent(lblNewLabel,
												GroupLayout.PREFERRED_SIZE, 61,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(textField,
												GroupLayout.PREFERRED_SIZE,
												105, GroupLayout.PREFERRED_SIZE)
										.addGap(43)
										.addComponent(lblAnswer,
												GroupLayout.PREFERRED_SIZE, 61,
												GroupLayout.PREFERRED_SIZE)
										.addGap(18)
										.addComponent(textField_1,
												GroupLayout.PREFERRED_SIZE,
												191, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.RELATED, 50,
												Short.MAX_VALUE)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																btnOpenTxtFile,
																GroupLayout.PREFERRED_SIZE,
																39,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																btnNewButton,
																GroupLayout.PREFERRED_SIZE,
																39,
																GroupLayout.PREFERRED_SIZE))
										.addGap(47)));
		setLayout(groupLayout);

	}
	/**
	 * Method to convert forward slashes into backslashes.
	 * @author Syrym*/
	private String reverseFWDSlashes(String path) {

		while (path.indexOf('\\') > -1) {
			int BSlashInd = path.indexOf('\\');
			path = path.substring(0, BSlashInd) + "/"
					+ path.substring(++BSlashInd);
		}
		return path;
	}
	/**
	 * @author Eva Revear
	 * 
	 *         date 12-10-14
	 * 
	 * @param theNote
	 *            a note containing a question and potentially an answer This
	 *            method takes in the notes, adds them to a list of notes, then
	 *            prints those notes to the analyst's files
	 * 
	 * */
	public void addNote(Notes theNote) throws IOException {
		Notes note = theNote;
		notes.add(note);

		File file = new File(dbfilesPath + myUserName + "/notes.txt");

		BufferedWriter writer = new BufferedWriter(new FileWriter(file));
		for (int i = 0; i < notes.size(); i++) {
			writer.write("Question: " + notes.get(i).getQuestion() + "\n "
					+ "Answer: " + notes.get(i).getAnswer() + " \n\n");
		}
		writer.close();
	}
	/**
	 * This method takes in information about the currently logged in analyst
	 * particularly the user name
	 * 
	 * @author Syrym
	 * 
	 * */

	@Override
	public void update(Observable o, Object arg) {
		Packet packet = (Packet) arg;
		if (o instanceof Analyst && packet.getTag().equals("a_username_qp")) {
			this.myUserName = packet.getUsername();
		}
	}
}
